import { defineStore } from 'pinia';
import { ref } from 'vue';
import axios from 'axios';
// 设置axios的基础URL
axios.defaults.baseURL = 'https://www.you04.cn';
export const useSystemStore = defineStore('system', () => {
    const token = ref(sessionStorage.getItem('token') || localStorage.getItem('token'));
    
    //系统配置信息获取
    const getSystemConfig = async () => {
        try {
            const response = await axios.get('/api/system-configs', {
                headers: {
                    'Authorization': `Bearer ${token.value}`
                }
            });
            return response.data.data;
        } catch (error) {
            console.error('获取系统配置失败:', error);
            throw new Error(error.response?.data?.message || 'fetch_system_config_failed');
        }
    }

    // 批量更新系统配置
    const batchUpdateSystemConfig = async (configs) => {
        try {
            const response = await axios.put('/api/system-configs/batch', configs, {
                headers: {
                    'Authorization': `Bearer ${token.value}`,
                    'Content-Type': 'application/json'
                }
            });
            return response.data;
        } catch (error) {
            console.error('批量更新系统配置失败:', error);
            throw new Error(error.response?.data?.message || 'batch_update_system_config_failed');
        }
    }

    // 单个更新系统配置
    const updateSystemConfig = async (id, config) => {
        try {
            const response = await axios.put(`/api/system-configs/${id}`, config, {
                headers: {
                    'Authorization': `Bearer ${token.value}`,
                    'Content-Type': 'application/json'
                }
            });
            return response.data;
        } catch (error) {
            console.error('更新系统配置失败:', error);
            throw new Error(error.response?.data?.message || 'update_system_config_failed');
        }
    }
    
    return {
        getSystemConfig,
        batchUpdateSystemConfig,
        updateSystemConfig
    }
})